import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 全屏遮罩广告弹窗：居中展示一张图片，图片下方只有关闭按钮（无白色底、无确认/取消文案）。
/// 返回值：点击图片返回 true，点击关闭返回 false/null。
Future<bool?> showAdDialog({
  required BuildContext context,
  required String imageUrl,
  VoidCallback? onImageTap,
}) {
  return showGeneralDialog<bool>(
    context: context,
    barrierDismissible: false,
    barrierLabel: 'ad_dialog',
    barrierColor: Colors.black54,
    pageBuilder: (_, __, ___) => const SizedBox.shrink(),
    transitionBuilder: (ctx, anim, __, ___) {
      final curved = Curves.easeOutBack.transform(anim.value);
      return Opacity(
        opacity: anim.value,
        child: Transform.scale(
          scale: 0.9 + 0.1 * curved,
          child: Center(
            child: Padding(
              padding: EdgeInsets.only(bottom: 60.w),
              child: Material(
                color: Colors.transparent,
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    InkWell(
                      onTap: () {
                        onImageTap?.call();
                        Navigator.pop(context, true);
                      },
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(12),
                        child: ConstrainedBox(
                          constraints: BoxConstraints(
                            maxWidth: 320.w,
                            maxHeight: 320.w,
                          ),
                          child: AspectRatio(
                            aspectRatio: 1,
                            child: Image.network(
                              imageUrl,
                              fit: BoxFit.fitWidth,
                              errorBuilder: (_, __, ___) => Container(
                                color: Colors.grey.shade300,
                                alignment: Alignment.center,
                                child: Icon(
                                  Icons.image,
                                  size: 48.w,
                                  color: Colors.white70,
                                ),
                              ),
                            ),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(height: 8.h),
                    GestureDetector(
                      onTap: () => Navigator.pop(context, false),
                      child: Container(
                        width: 44.w,
                        height: 44.w,
                        decoration: BoxDecoration(
                          color: Colors.black54,
                          shape: BoxShape.circle,
                        ),
                        child: const Icon(Icons.close, color: Colors.white),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      );
    },
  );
}
